Sitecore versions: Expected to work with 5.1.1.x and 5.2.x releases.
We want to associate a content item with an extranet user, to give each user a personal site. Is it possible to extend the user template with an extra field of type internal link? Our intention is to use this field to map the user to a content item.
Follow the steps below to extend the user template with an extra field of the internal link type:
Compile the following code and place into the bin folder:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Text;
using Sitecore;
using Sitecore.Diagnostics;
using Sitecore.SecurityModel;
using Sitecore.Configuration;
using Sitecore.Caching;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Data.Fields;
using Sitecore.Text;
using Sitecore.Web;
using Sitecore.Web.UI.HtmlControls;
using Sitecore.Web.UI.WebControls;
using Sitecore.Web.UI.Sheer;
namespace Sitecore.Shell.Applications.Security.EditUser
{
public class CustomEditUserForm : EditUserForm
{
// the Edit control named "Link" was created on the fly
// since we extended the security templates file
private Sitecore.Web.UI.HtmlControls.Edit LinkEditControl;
private void InstantiateLinkEditControl()
{
// searching the Edit control named "Link" in the fields control collection
foreach (Control ctrl in this.Fields.Controls)
{
if ((ctrl is Sitecore.Web.UI.HtmlControls.Edit) && (ctrl.ID == "Link"))
{
LinkEditControl = (Sitecore.Web.UI.HtmlControls.Edit)ctrl;
}
}
}
[HandleMessage("user:insertlink", true)]
protected void InsertLink(ClientPipelineArgs args)
{
InstantiateLinkEditControl();
// this method will be called when the InternalLinkBrowse botton is clicked
if (LinkEditControl != null)
{
// catching the postback
if (args.IsPostBack)
{
if (((args.Result != null) && (args.Result.Length > 0)) && (args.Result != "undefined"))
{
// setting the value of the textbox to the return value of the dialog
// here the selected item's ID is transformed into the item full path
LinkEditControl.Value = Factory.GetDatabase("master").Items[args.Result].Paths.FullPath;
}
}
else
{
// calling the item browser application
UrlString text = new UrlString("/sitecore/shell/Applications/Item browser.html");
// getting the item from the path in the Edit control named "Link"
Item refItem = Factory.GetDatabase("master").Items[LinkEditControl.Value];
if (refItem != null)
{
// if this item exists, passing it's Id to the dialog
// the dialog will show this item by default
text.Add("id", refItem.ID.ToString());
}
// showing the dialog itself
Sitecore.Context.ClientPage.ClientResponse.ShowModalDialog(text.ToString(), true);
args.WaitForPostBack();
}
}
}
}
}
The result is shown in the screenshot below.